home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / Codex ass dev system v1.10.adf / inc / Astartup.asm
Assembly Source File  |  1988-09-19  |  7KB  |  352 lines

  1.  
  2. ************************************************************************
  3. *
  4. *    C Program Startup/Exit (Combo Version: CLI and WorkBench)
  5. *
  6. ************************************************************************
  7. *
  8. *   Source Control:
  9. *
  10. *       $Header$
  11. *
  12. *       $Locker$
  13. *
  14. *       $Log$
  15. *
  16. ************************************************************************
  17.  
  18.  
  19. ******* Included Files *************************************************
  20.  
  21.     INCLUDE    "exec/types.i"
  22.     INCLUDE "exec/alerts.i"
  23.     INCLUDE "exec/nodes.i"
  24.     INCLUDE "exec/lists.i"
  25.     INCLUDE "exec/ports.i"
  26.     INCLUDE "exec/libraries.i"
  27.     INCLUDE "exec/tasks.i"
  28.     INCLUDE "libraries/dos.i"
  29.     INCLUDE "libraries/dosextens.i"
  30.     INCLUDE "workbench/startup.i"
  31.  
  32.  
  33. ******* Imported *******************************************************
  34.  
  35. xlib    macro
  36.     xref    _LVO\1
  37.     endm
  38.  
  39.     xref    _AbsExecBase
  40.     xref    _Input
  41.     xref    _Output
  42.  
  43.     xref    _main            ; C code entry point
  44.  
  45.     xlib    Alert
  46.     xlib    FindTask
  47.     xlib    Forbid
  48.     xlib    GetMsg
  49.     xlib    OpenLibrary
  50.     xlib    CloseLibrary
  51.     xlib    ReplyMsg
  52.     xlib    Wait
  53.     xlib    WaitPort
  54.  
  55.     xlib    CurrentDir
  56.     xlib    Open
  57.  
  58.  
  59. ******* Exported *******************************************************
  60.  
  61.     xdef    _SysBase
  62.     xdef    _DOSBase
  63.  
  64.     xdef    _errno
  65.     xdef    _stdin
  66.     xdef    _stdout
  67.     xdef    _stderr
  68.  
  69.     xdef    _exit            ; standard C exit function
  70.  
  71.  
  72. callsys    macro
  73.     CALLLIB    _LVO\1
  74.     endm
  75.  
  76.  
  77. ************************************************************************
  78. *
  79. *    Standard Program Entry Point
  80. *
  81. ************************************************************************
  82. *
  83. *    main (argc, argv)
  84. *       int  argc;
  85. *       char *argv[]; 
  86. *
  87. ************************************************************************
  88.  
  89. startup:                ; reference for Wack users
  90.         move.l    sp,initialSP    ; initial task stack pointer
  91.         move.l    d0,dosCmdLen
  92.         move.l    a0,dosCmdBuf
  93.         clr.l    returnMsg
  94.  
  95.     ;------ get Exec's library base pointer:
  96.         move.l    _AbsExecBase,a6
  97.         move.l    a6,_SysBase
  98.  
  99.     ;------ get the address of our task
  100.         suba.l    a1,a1
  101.         callsys    FindTask
  102.         move.l    d0,a4
  103.  
  104.     ;------ are we running as a son of Workbench?
  105.         tst.l    pr_CLI(A4)
  106.         beq    fromWorkbench
  107.  
  108. ;=======================================================================
  109. ;====== CLI Startup Code ===============================================
  110. ;=======================================================================
  111. fromCLI:
  112.     ;------    attempt to open DOS library:
  113.         bsr    openDOS
  114.  
  115.     ;------ find command name:
  116.         move.l    pr_CLI(a4),a0
  117.         add.l   a0,a0        ; bcpl pointer conversion
  118.         add.l   a0,a0
  119.         move.l    cli_CommandName(a0),a0
  120.         add.l   a0,a0        ; bcpl pointer conversion
  121.         add.l   a0,a0
  122.  
  123.     ;------ create buffer and array:
  124. *        link    a6,#-(100+16*4+2*4)
  125.         movem.l    d2/a2/a3,-(sp)
  126.         lea    argvBuffer,a2
  127.         lea    argvArray,a3
  128. *        move.l    a3,16(sp)    ; save 
  129.         moveq.l    #1,d2        ; param counter
  130.  
  131.     ;------ fetch command name:
  132.         moveq.l    #0,d0
  133.         move.b    (a0)+,d0    ; size of command name
  134.         move.l    a2,(a3)+    ; ptr to command name
  135.         bra.s    1$
  136. 2$:        move.b    (a0)+,(a2)+
  137. 1$:        dbf    d0,2$
  138.         clr.b    (a2)+
  139.  
  140.     ;------    collect parameters:
  141.         move.l    dosCmdLen,d0
  142.         move.l    dosCmdBuf,a0
  143.  
  144.     ;------ skip control characters and space:
  145. 3$:        move.b    (a0)+,d1
  146.         subq.l    #1,d0
  147.         ble.s    parmExit
  148.         cmp.b    #' ',d1
  149.         ble.s    3$
  150.  
  151.     ;------ copy parameter:
  152.         addq.l    #1,d2
  153.         move.l    a2,(a3)+
  154.         bra.s    5$
  155. 4$:        move.b    (a0)+,d1
  156.         subq.l    #1,d0
  157.         cmp.b    #' ',d1
  158.         ble.s    6$
  159. 5$:        move.b    d1,(a2)+
  160.         bra.s    4$
  161. 6$:
  162.         clr.b    (a2)+
  163.         bra.s    3$
  164. parmExit:    clr.b    (a2)+
  165.         clr.l    (a3)+
  166.  
  167.         move.l    d2,d0
  168.         movem.l    (sp)+,d2/a2/a3
  169.         pea    argvArray
  170.         move.l    d0,-(sp)
  171.  
  172.  
  173. *
  174. *  The above code relies on the end of line containing a control
  175. *  character of any type, i.e. a valid character must not be the
  176. *  last.  This fact is ensured by DOS.
  177. *
  178.         
  179.  
  180.     ;------ get standard input handle:
  181.         jsr    _Input
  182.         move.l    d0,_stdin
  183.  
  184.     ;------ get standard output handle:
  185.         jsr    _Output
  186.         move.l    d0,_stdout
  187.         move.l    d0,_stderr
  188.  
  189.     ;------ call C main entry point
  190.         jsr    _main
  191.  
  192.     ;------ return success code:
  193.         moveq.l    #0,D0
  194.         move.l    initialSP,sp    ; restore stack ptr
  195.         rts
  196.  
  197. ;=======================================================================
  198. ;====== Workbench Startup Code =========================================
  199. ;=======================================================================
  200. fromWorkbench:
  201.     ;------ open the DOS library:
  202.         bsr    openDOS
  203.  
  204.     ;------ we are now set up.  wait for a message from our starter
  205.         bsr    waitmsg
  206.  
  207.     ;------ save the message so we can return it later
  208.         move.l    d0,returnMsg    NOTE: no GetMsg performed
  209.  
  210.     ;------ push the message on the stack for wbmain
  211.         clr.l    -(SP)        indicate: run from Workbench
  212.         move.l    d0,-(SP)
  213.  
  214.     ;------ get the first argument
  215.         move.l    d0,a2
  216.         move.l    sm_ArgList(a2),d0
  217.         beq.s    docons
  218.  
  219.     ;------ and set the current directory to the same directory
  220.         move.l    _DOSBase,a6
  221.         move.l    d0,a0
  222.         move.l    wa_Lock(a0),d1
  223.         callsys    CurrentDir
  224.  
  225. docons:
  226.     ;------ get the toolwindow argument
  227.         move.l    sm_ToolWindow(A2),d1
  228.         beq.s    domain
  229.  
  230.     ;------ open up the file
  231.         move.l    #MODE_OLDFILE,d2
  232.         callsys    Open
  233.  
  234.     ;------ set the C input and output descriptors
  235.         move.l    d0,_stdin
  236.         move.l    d0,_stdout
  237.         move.l    d0,_stderr
  238.         beq.s    domain
  239.  
  240.     ;------ set the console task (so Open( "*", mode ) will work
  241.     ;    waitmsg has left the task pointer in A4 for us
  242.         lsl.l    #2,d0
  243.         move.l    d0,a0
  244.         move.l    fh_Type(a0),pr_ConsoleTask(A4)
  245.  
  246. domain:
  247.         jsr    _main
  248.         moveq.l    #0,d0        Successful return code
  249.         bra.s    exit2
  250.  
  251.  
  252. ************************************************************************
  253. *
  254. *    C Program Exit Function
  255. *
  256. ************************************************************************
  257. *
  258. *  Warning: this function really needs to do more than this.
  259. *
  260. ************************************************************************
  261.  
  262. _exit:
  263.         move.l    4(SP),d0    ; extract return code
  264. exit2:
  265.         move.l  initialSP,SP    ; restore stack pointer
  266.         move.l    d0,-(SP)    ; save return code
  267.  
  268.     ;------ close DOS library:
  269.         move.l    _AbsExecBase,A6
  270.         move.l    _DOSBase,d0
  271.         beq.s    1$
  272.         move.l    d0,a1
  273. 1$:        callsys    CloseLibrary
  274.  
  275.     ;------ if we ran from CLI, skip workbench cleanup:
  276.         tst.l    returnMsg
  277.         beq.s    exitToDOS
  278.  
  279.     ;------ return the startup message to our parent
  280.     ;    we forbid so workbench can't UnLoadSeg() us
  281.     ;    before we are done:
  282.         callsys Forbid
  283.         move.l    returnMsg,a1
  284.         callsys    ReplyMsg
  285.  
  286.     ;------ this rts sends us back to DOS:
  287. exitToDOS:
  288.         move.l    (SP)+,d0
  289.         rts
  290.  
  291.  
  292. ;-----------------------------------------------------------------------
  293. noDOS:
  294.         ALERT    (AG_OpenLib!AO_DOSLib)
  295.         moveq.l    #100,d0
  296.         bra.s    exit2
  297.  
  298.  
  299. ;-----------------------------------------------------------------------
  300. ; This routine gets the message that workbench will send to us
  301. ; called with task id in A4
  302.  
  303. waitmsg:
  304.         lea    pr_MsgPort(A4),a0      * our process base
  305.         callsys    WaitPort
  306.         lea    pr_MsgPort(A4),a0      * our process base
  307.         callsys GetMsg
  308.         rts
  309.  
  310. ;-----------------------------------------------------------------------
  311. ;  Open the DOS library:
  312.  
  313. openDOS
  314.         clr.l    _DOSBase
  315.         lea    DOSName,A1
  316.         move.l    #LIBRARY_VERSION,d0
  317.         callsys OpenLibrary
  318.         move.l    D0,_DOSBase
  319.         beq    noDOS
  320.         rts
  321.  
  322.  
  323. ************************************************************************
  324.  
  325.     DATA
  326.  
  327. ************************************************************************
  328.  
  329. VerRev        dc.w    1,0
  330.  
  331. _SysBase    dc.l    0
  332. _DOSBase    dc.l    0
  333.  
  334. _errno        dc.l    0
  335. _stdin        dc.l    0
  336. _stdout        dc.l    0
  337. _stderr        dc.l    0
  338.  
  339. initialSP    dc.l    0
  340. returnMsg    dc.l    0
  341.  
  342. dosCmdLen    dc.l    0
  343. dosCmdBuf    dc.l    0
  344.  
  345. argvArray    ds.l    32
  346. argvBuffer    ds.b    256
  347.  
  348. DOSName        DOSNAME
  349.  
  350.  
  351.     END
  352.